function dee_date(class_prefix)
{
	this.class_prefix=class_prefix;
	this.today_year=false;
	this.today_date=false;
	this.today_month=false;
	this.shown=false;
	this.last_obj=false;
	this.wrapper=false;
	this.time=false;
	this.months = 'Январь,Февраль,Март,Апрель,Май,Июнь,Июль,Август,Сентябрь,Октябрь,Ноябрь,Декабрь'.split(',');
	/*День недели*/
	this.getDay =function(day,mon,year){
		var days = new Array(7,1,2,3,4,5,6);
		day=parseInt(day, 10); //если день двухсимвольный и <10
		mon=parseInt(mon, 10); //если месяц двухсимвольный и <10
		var a=parseInt((14-mon)/12);
		var y=year-a;
		var m=mon+12*a-2;
		var d=(7000+parseInt(day+y+parseInt(y/4)-parseInt(y/100)+parseInt(y/400)+(31*m)/12))%7;
		return days[d];
	};
	/*Количество дней в месяце*/
	this.getMothDays =function(year,month)
	{
		var leap_year=(year%100==0) ? (year%400==0) : (year%4==0);//Високосный год
		switch(month)
		{
			case 4: case 6:	case 9:	case 11: return 30;
			case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
			case 2: return leap_year?29:28;
		}
	};
	this.getMonth =function(year,month)
	{

		w=this.getDay(1,month+1,year);
		days=this.getMothDays(year,month+1)+1;
		table='<tbody>';
		for(i=1;i<w;i++)
		{
			if(i==1)table+='<tr>';
			table+='<td class="'+this.class_prefix+'empty"></td>';
		}
		for(d=1;d<days;d++)
		{
			if(w==1)
			{
				if(d!=1)table+='</tr>';
				table+='<tr>';
			}
			t_class=this.class_prefix+'day';
			if(year== this.today_year&&month==this.today_month && d==this.today_date)	t_class+=' '+this.class_prefix+'today';
			if(w>5)	t_class+=' '+this.class_prefix+'weekend';
			table+='<td class="'+t_class+'" onclick="dee_date_time_object.set_date(this)">'+d+'</td>';
			if(++w>7)w=1;
		}
		if(w!=1)
		for(i=w;i<7;i++)
		{
			table+='<td class="'+this.class_prefix+'empty"></td>';
		}
		return table+'</tr></tbody>';
	};
	this.change_month =function(obj,dig)
	{
		tab=$(obj).parents('.'+this.class_prefix+'month');
		y=tab.find('.'+this.class_prefix+'year').text()*1;
		m=tab.attr('m')*1+dig;
		if(m>11){y++; m=0;}
		if(m<0){y--; m=11;}
		tab.find('tbody').replaceWith(this.getMonth(y,m)).end()
			.find('.'+this.class_prefix+'month_name').html(this.months[m]);
		tab.attr({'m':m}).find('.'+this.class_prefix+'year').text(y);
		//elog(y+' '+m);
	};
	this.change_year =function(obj)
	{
		obj=$(obj);
		year=obj.text()*1;
		obj.parents('.'+this.class_prefix+'month').find('tbody').replaceWith(this.get_years(year));

	};
	this.change_years =function(obj,dig)
	{
		obj=$(obj);
		year=obj.parent().find('.'+this.class_prefix+'central').text()*1+(9*dig);
		obj.parents('.'+this.class_prefix+'month').find('tbody').replaceWith(this.get_years(year));

	};
	this.get_years =function(year)
	{
		table='<tbody><tr><td onclick="dee_date_time_object.change_years(this,-1)" class="'+this.class_prefix+'arrow">«</td><td colspan="5"  class="'+this.class_prefix+'years">';
		for(i=year-4;i<year+5;i++)
		{
			t_class='';
			if(i==this.today_year)t_class+=' '+this.class_prefix+'today';
			if(year==i)t_class+=' '+this.class_prefix+'central';
			if(t_class!='')t_class='class="'+t_class+'"';
			table+='<div onclick="dee_date_time_object.set_year(this)"'+t_class+'>'+i+'</div>';
		}
		return table+'<div style="clear:both"></div></td><td onclick="dee_date_time_object.change_years(this,1)" class="'+this.class_prefix+'arrow">»</td></tr></tbody>';
	};
	this.set_year =function(obj)
	{
		obj=$(obj);
		obj.parents('.'+this.class_prefix+'month').find('.'+this.class_prefix+'year').text(obj.text());
		this.change_month(obj,0);
	};
	this.change_months =function(obj)
	{
		obj=$(obj);
		table='<tbody><tr><td colspan="7" class="'+this.class_prefix+'months">';
		for(i=0;i<12;i++)
		table+='<div onclick="dee_date_time_object.set_month(this,'+i+')">'+this.months[i]+'</div>';
		table+='<div style="clear:both"></div></td></tr></tbody>';
		obj.parents('.'+this.class_prefix+'month').find('tbody').replaceWith(table);
	};
	this.set_month =function(obj,mon)
	{
		obj=$(obj);
		obj.parents('.'+this.class_prefix+'month').attr('m',mon).find('.'+this.class_prefix+'month_name').text(obj.text());
		this.change_month(obj,0);
	};
	this.change_hour =function(obj)
	{
		obj=$(obj);
		table='<tbody><tr><td colspan="7" class="'+this.class_prefix+'hours">';
		for(i=0;i<24;i++)
		table+='<div onclick="dee_date_time_object.set_hour(this)">'+(i<10?'0':'')+i+'</div>';
		table+='<div style="clear:both"></div></td></tr></tbody>';
		obj.parents('.'+this.class_prefix+'month').find('tbody').replaceWith(table);
	};
	this.change_minutes =function(obj)
	{
		obj=$(obj);
		table='<tbody><tr><td colspan="7" class="'+this.class_prefix+'minutes">';
		for(i=0;i<60;i++)
		table+='<div onclick="dee_date_time_object.set_minutes(this)">'+(i<10?'0':'')+i+'</div>';
		table+='<div style="clear:both"></div></td></tr></tbody>';
		obj.parents('.'+this.class_prefix+'month').find('tbody').replaceWith(table);
	};
	this.set_hour =function(obj)
	{
		obj=$(obj);
		obj.parents('.'+this.class_prefix+'month').find('.'+this.class_prefix+'time .'+this.class_prefix+'hours').text(obj.text());
		this.change_month(obj,0);
	};
	this.set_minutes =function(obj)
	{
		obj=$(obj);
		obj.parents('.'+this.class_prefix+'month').find('.'+this.class_prefix+'time .'+this.class_prefix+'minutes').text(obj.text());
		this.change_month(obj,0);
	};
	this.set_date=function(obj)
	{
		day=$(obj).text();
		d=this.shown;
		m=d.attr('m')*1+1;
		this.last_obj.val(d.find('.'+this.class_prefix+'year').text()+'-'+(m<10?'0':'')+m+'-'+(day<10?'0':'')+day+(this.time?' '+d.find('.'+this.class_prefix+'time').text()+':00':''));
		this.hide();
	};
	this.hide=function()
	{
		this.shown.remove();
		this.time=false;
		this.shown=false;
		this.last_obj=false;
		this.wrapper.remove();
	};
	this.reset=function()
	{
		this.last_obj.val('');
		this.hide();
	};
	this.set_now=function()
	{
		date=new Date();
		m=date.getMonth()+1;
		day=date.getDate();
		this.last_obj.val((date.getYear()+1900)+'-'+(m<10?'0':'')+m+'-'+(day<10?'0':'')+day+(this.time?' '+date.toTimeString().replace(/^(\d+:\d+:\d+).+$/,'$1'):''));
		this.hide();
	};
	this.show =function(obj,time)
	{
		if(this.shown)
			this.hide();
		obj=$(obj);
		if(obj.hasClass('disabled'))return;
		this.last_obj=obj;
		val=obj.val();
		var today=new Date();
		var now_text='Сегодня';
		if(val && val!='')
			today=new Date(val.replace(/^(\d+)-(\d+)-(\d+)/, '$2/$3/$1'));
		this.today_year=today.getYear()+1900;
		this.today_date=today.getDate();
		this.today_month=today.getMonth();
		if(time)
		{
			this.time=true;
			now_text='Сейчас';
			time=today.toTimeString().replace(/^(\d+):(\d+).+$/,'<span onclick="dee_date_time_object.change_hour(this)" class="'+this.class_prefix+'hours">$1</span>:<span onclick="dee_date_time_object.change_minutes(this)" class="'+this.class_prefix+'minutes">$2</span>');
		}
		else time='';
		table='<table class="'+this.class_prefix+'month" m="'+this.today_month+'"><thead><tr><td colspan="2" class="'+this.class_prefix+'today_btn" onclick="dee_date_time_object.set_now()">'+now_text+'</td><td colspan="3" class="'+this.class_prefix+'time">'+time+'</td><td colspan="2" class="'+this.class_prefix+'reset" onclick="dee_date_time_object.reset()">Сброс </td></tr><tr><th colspan="2" onclick="dee_date_time_object.change_month(this,-1)" class="'+this.class_prefix+'arrow">«</th><th colspan="3"><div class="'+this.class_prefix+'month_name" onclick="dee_date_time_object.change_months(this)">'+this.months[this.today_month]+'</div><div class="'+this.class_prefix+'year" onclick="dee_date_time_object.change_year(this)">'+this.today_year+'</div></th><th colspan="2"  onclick="dee_date_time_object.change_month(this,1)" class="'+this.class_prefix+'arrow">»</th></tr><tr class="'+this.class_prefix+'title_days"><td>Пн</td><td>Вт</td><td>Ср</td><td>Чт</td><td>Пт</td><td class="'+this.class_prefix+'weekend">Сб</td><td class="'+this.class_prefix+'weekend">Вс</td></tr></thead>'
			+this.getMonth(this.today_year,this.today_month)
		+'</table>';
		this.wrapper=$('<div class="'+this.class_prefix+'wrapper" onclick="dee_date_time_object.hide();"></div>').appendTo('body');
		this.shown=$(table).css('display','none').appendTo('body');
		offset=obj.offset();
		tab_h=this.shown.height();
		inp_h=obj.height();
		top_of=offset.top;
		left_of=offset.left;
		scroll_top=$(document).scrollTop();
		if(scroll_top+$(window).height()<top_of+tab_h)
			top_of-=(tab_h+inp_h);
		else top_of+=inp_h;
		this.shown.css({'top':top_of+'px','left':left_of+'px'}).fadeIn();
	};
}
var dee_date_time_object=new dee_date('dee_date_');